#import "RNListItem.h"
#import "RNItem.h"
#import "RNCategory.h"
#import "RNList.h"
#import "RNAisle.h"
#import "NSManagedObject+SRAdditions.h"
#import "RNConstants.h"
#import "RNFavorites.h"

@implementation RNListItem 

@dynamic notes;
@dynamic count;
@dynamic complete;
@dynamic list;
@dynamic tags;
@dynamic item;
@dynamic category;
@dynamic aisles;

+(NSSet *)keys {
    return [NSSet setWithObjects:@"title", @"count", @"complete", @"notes", @"tags", @"favorite", @"list", nil];
}

+(NSSet *)keyPathsForValuesAffectingObjectDictionary {
    return [self keys];
}

//+(NSSet *)keyPathsForValuesAffectingListTitle {
//    return [NSSet setWithObject:@"list.title"];
//}

//-(NSString *)listTitle {
//    return [[self list] title];
//}

//-(void)setListTitle:(NSString *)title {
//    [[self list] setTitle:title];
//}

-(NSNumber *)favorite {
    return [[self item] favorite];
}

-(void)setFavorite:(NSNumber *)favoriteValue {
    [self willChangeValueForKey:@"favorite"];
    [[self item] setFavorite:favoriteValue];
    [self didChangeValueForKey:@"favorite"];
}

-(NSString *)title {
    return [[self item] title];
}

-(void)setTitle:(NSString *)titleValue {
    [self willChangeValueForKey:@"title"];
    [[self item] setTitle:titleValue];
    [self didChangeValueForKey:@"title"];
}

+(NSSet *)keyPathsForValuesAffectingStores {
    return [NSSet setWithObject:@"aisles"];
}

-(NSSet *)stores {
    NSSet *stores = [[self aisles] valueForKey:@"store"];
    return stores;
}

-(NSDictionary *)objectDictionary {
    return [self dictionaryWithValuesForKeys:[[RNListItem keys] allObjects]];
}

-(NSUInteger)displayIndex {
    return 0;
}

-(NSArray *)children {
    return nil;
}

-(NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *dictionaryRepresentation = [NSMutableDictionary dictionaryWithDictionary:[self objectDictionary]];
    [dictionaryRepresentation setValue:[[self category] URIRepresentation] forKey:@"category"];
    [dictionaryRepresentation setValue:[[self item] URIRepresentation] forKey:@"item"];
    [dictionaryRepresentation setValue:[[self list] URIRepresentation] forKey:@"list"];
    NSMutableSet *aisleURIs = [NSMutableSet set];
    for (NSManagedObject *aisle in [self aisles]) 
        [aisleURIs addObject:[aisle URIRepresentation]];
    [dictionaryRepresentation setValue:aisleURIs forKey:@"aisles"];
    return dictionaryRepresentation;
}

-(void)moveToContainer:(NSManagedObject *)container {
    if ([container isKindOfClass:[RNCategory class]]) {
        RNCategory *destinationCategory = (RNCategory *)container;
        [[self item] setCategory:destinationCategory];
        [[[self item] listItems] makeObjectsPerformSelector:@selector(setCategory:) withObject:container];
        return;
    }
    if ([container isKindOfClass:[RNList class]]) {
        [self setList:(RNList *)container];
        return;
    }
    if ([container isKindOfClass:[RNAisle class]]) {
        RNAisle *destinationAisle = (RNAisle *)container;
        NSSet *aislesInTheSameStore = [[self aisles] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"store == %@", [destinationAisle store]]];
        if ([aislesInTheSameStore count] > 0) {
            [[self item] removeAisles:aislesInTheSameStore];
            [[[self item] listItems] makeObjectsPerformSelector:@selector(removeAisles:) withObject:aislesInTheSameStore];
        }
        
        [[self item] addAislesObject:destinationAisle];
        [[[self item] listItems] makeObjectsPerformSelector:@selector(addAislesObject:) withObject:destinationAisle];
    }
    if ([container isKindOfClass:[RNFavorites class]]) {
        [self setFavorite:[NSNumber numberWithBool:YES]];
    }
}

@end
